<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-card">
        <div class="login-header">
          <h2>登录</h2>
          <p>欢迎使用伟邦人才信息网</p>
        </div>
        
        <el-form
          :model="loginForm"
          :rules="loginRules"
          ref="loginFormRef"
          class="login-form"
          label-width="0"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              prefix-icon="User"
              size="large"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              prefix-icon="Lock"
              size="large"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="large"
              class="login-btn"
              @click="handleLogin"
              :loading="loading"
              block
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
        
        <!--<div class="login-footer">-->
          <!--<p>还没有账号？</p>-->
          <!--<router-link to="/register" class="register-link">立即注册</router-link>-->
        <!--</div>-->
        
        <!--<div class="demo-accounts">-->
          <!--<h4>演示账号</h4>-->
          <!--<div class="account-list">-->
            <!--<div class="account-item">-->
              <!--<span>管理员：</span>-->
              <!--<span>admin / 123456</span>-->
            <!--</div>-->
            <!--<div class="account-item">-->
              <!--<span>业务员A：</span>-->
              <!--<span>sales001 / 123456</span>-->
            <!--</div>-->
            <!--<div class="account-item">-->
              <!--<span>业务员B：</span>-->
              <!--<span>sales002 / 123456</span>-->
            <!--</div>-->
          <!--</div>-->
        <!--</div>-->
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { mockUsers } from '@/mock/data'
import { ElMessage } from 'element-plus'
import { login,getInfo, getRouters } from "@/api/login"

const router = useRouter()
const userStore = useUserStore()

const loginFormRef = ref()
const loading = ref(false)
//admin123
const loginForm = ref({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}
const getUserInfo = async () => {
  const userInfo = await getInfo()
  const routeInfo = await getRouters()

  userStore.setUser(userInfo)
  userStore.setRoute(routeInfo)

    ElMessage.success('登录成功')
      // 根据角色跳转
      if (userInfo?.roles?.[0] === 'admin') {
        router.push('/admin')
      } else {
        router.push('/user')
      }
}

const handleLogin = async () => {
  try {
    await loginFormRef.value.validate()
    loading.value = true

    // 模拟登录验证
//    const user = mockUsers.find(u =>
//      u.username === loginForm.value.username &&
//      u.password === loginForm.value.password
//    )

      const userObj = await login(loginForm.value)
      const userData = {
        token: userObj.token
      }
      userStore.setToken(userData)
      getUserInfo()
  } catch (error) {
    ElMessage.error('请完善登录信息')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
    /deep/ .el-form-item__content{
        justify-content: center;
    }

    .login-btn {

        width: 200px;
    }
.login-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 400px;
}

.login-card {
  background: white;
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #333;
  margin: 0 0 10px 0;
  font-size: 28px;
}

.login-header p {
  color: #666;
  margin: 0;
  font-size: 14px;
}

.login-form {
  margin-bottom: 20px;
}

.login-footer {
  text-align: center;
  margin-bottom: 30px;
}

.login-footer p {
  color: #666;
  margin: 0 0 10px 0;
  font-size: 14px;
}

.register-link {
  color: #667eea;
  text-decoration: none;
  font-weight: bold;
}

.register-link:hover {
  text-decoration: underline;
}

.demo-accounts {
  border-top: 1px solid #eee;
  padding-top: 20px;
}

.demo-accounts h4 {
  color: #333;
  margin: 0 0 15px 0;
  font-size: 16px;
  text-align: center;
}

.account-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.account-item {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #666;
  padding: 5px 0;
}

.account-item span:first-child {
  font-weight: bold;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-card {
    padding: 30px 20px;
  }
  
  .login-header h2 {
    font-size: 24px;
  }
}
</style> 